package servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.award.jdbc.dao.JdbcDao;
import com.award.model.Commodity;
import com.award.model.Relationship;
import com.award.model.User;
import com.award.model.Winner;

public class pickUp extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public pickUp() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		User user = null;
		JdbcDao jd = new JdbcDao();
		int result = 0;
		Winner winner = null;
		if (null == request.getSession().getAttribute("user")) {
			response.sendRedirect(request.getContextPath()
					+ "/jsp/connectionWechat.jsp");
		} else {
			user = (User) request.getSession().getAttribute("user");
			Relationship r = new Relationship();
			int commodityID = Integer.parseInt(request
					.getParameter("commodityID"));
			r.setChecked(0);
			r.setCommodityID(commodityID);
			r.setUserID(user.getId());
			r.setSelectNum(Integer.parseInt(request.getParameter("selectNum")));
			Commodity thiscd = null;
			try {
				jd.insertRelationship(r);
				jd.addCommodityNum(commodityID);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				thiscd = jd.queryCommodityByID(commodityID);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			// static获取
			// result = Commodity.judge(jd.queryCommodityByID(commodityID)
			// .getSelectPeopleNum(), commodityID);
			result = thiscd.judge();
			System.out.println(result);
			if (-1 == result)
				response.sendRedirect(request.getContextPath()
						+ "/jsp/raffle.jsp?CommodityID=" + commodityID);
			else {
				winner = new Winner(commodityID, result);
				try {
					jd.insertWinner(winner);
					jd.openAward(commodityID, result);
					jd.oneReffle(result, commodityID);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				response.sendRedirect(request.getContextPath()
						+ "/jsp/raffle.jsp?CommodityID=" + commodityID);
			}
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
